package com.system.exam.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.exam.admin.common.utils.StringUtils;
import com.system.exam.admin.core.customBean.LoginLogBean;
import com.system.exam.admin.core.customBean.OperationLogBean;
import com.system.exam.admin.core.http.Result;
import com.system.exam.admin.entity.SysLoginLog;
import com.system.exam.admin.entity.SysOperationLog;
import com.system.exam.admin.service.SysLoginLogService;
import com.system.exam.admin.service.SysOperationLogService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/log")
@Api(tags = "系统日志")
public class SysLogController {

    @Autowired
    private SysLoginLogService sysLoginLogService;

    @Autowired
    private SysOperationLogService sysOperationLogService;

    @PostMapping("/loginList")
    @PreAuthorize("hasAuthority('system:loginLog:list')")
    public Result loginLogList(@RequestBody LoginLogBean loginLogBean) {
        String name = loginLogBean.getName();
        String ipAttribution = loginLogBean.getIpAttribution();
        String createStartTime = loginLogBean.getCreateStartTime();
        String createEndTime = loginLogBean.getCreateEndTime();

        Page<SysLoginLog> pageResult = sysLoginLogService.page(
                new Page<>(loginLogBean.getPageNum(),
                        loginLogBean.getPageSize()),
                new QueryWrapper<SysLoginLog>()
                        .like(StringUtils.isNotEmpty(name), "name", name)
                        .like(StringUtils.isNotEmpty(ipAttribution), "ip_attribution", ipAttribution)
                        .between("create_time", createStartTime, createEndTime)
                        .orderByDesc("create_time"));
        List<SysLoginLog> userList = pageResult.getRecords();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", userList);
        resultMap.put("total", pageResult.getTotal());
        return Result.success(resultMap);
    }

    @PostMapping("/operationList")
    @PreAuthorize("hasAuthority('system:operationLog:list')")
    public Result operationList(@RequestBody OperationLogBean operationLogBean) {
        String logType = operationLogBean.getLogType();
        String createUserName = operationLogBean.getCreateUserName();
        String createStartTime = operationLogBean.getCreateStartTime();
        String createEndTime = operationLogBean.getCreateEndTime();
        String deviceName = operationLogBean.getDeviceName();

        Page<SysOperationLog> pageResult = sysOperationLogService.page(
                new Page<>(operationLogBean.getPageNum(),
                        operationLogBean.getPageSize()),
                new QueryWrapper<SysOperationLog>()
                        .like(StringUtils.isNotEmpty(logType), "log_type", logType)
                        .like(StringUtils.isNotEmpty(createUserName), "create_user_name", createUserName)
                        .like(StringUtils.isNotEmpty(deviceName), "device_name", deviceName)
                        .between("create_time", createStartTime, createEndTime)
                        .orderByDesc("create_time"));
        List<SysOperationLog> userList = pageResult.getRecords();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", userList);
        resultMap.put("total", pageResult.getTotal());
        return Result.success(resultMap);
    }
}
